package com.lsh.exception;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SysExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        SysException ex; //获取异常对象
        if(e instanceof SysException) {
            ex = (SysException) e;
        }else{
            ex = new SysException("该异常正在调试中...");
        }
        ModelAndView mv = new ModelAndView();
        JSONObject jo = new JSONObject();
        jo.put("errorMsg", ex.getMessage());
        mv.addObject("data", jo.toString());
        mv.setViewName("/JSON");
        return mv;
    }
}
